import 'package:flutter/material.dart';
import 'package:flutter_demo/ui/pages/comic/home/widgets/popular.dart';
import 'package:flutter_demo/ui/pages/comic/home/widgets/recommend.dart';
import 'package:flutter_demo/ui/pages/comic/home/widgets/renew.dart';
import 'package:flutter_demo/ui/pages/status/provider/provider_test_widget.dart';
import 'package:get/get.dart';

import 'index.dart';

class HomePage extends StatefulWidget {
  static const routeName = "/HomePage";
  const HomePage({Key? key}) : super(key: key);

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage>
    with AutomaticKeepAliveClientMixin, SingleTickerProviderStateMixin {
  HomeController get controller => GetInstance().find<HomeController>();

  @override
  bool get wantKeepAlive => true;

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<HomeController>(
      init: HomeController(),
      id: "barChange",
      builder: (_) {
        return Scaffold(
          extendBodyBehindAppBar: true,
          body: _buildView(),
        );
      },
    );
  }

  // 主视图
  Widget _buildView() {
    return DefaultTabController(
        length: tabTitle.length,
        child: Scaffold(
          extendBodyBehindAppBar: true,
          appBar: AppBar(
            toolbarHeight: controller.ifShowSearch.value ? 80 : 40,
            centerTitle: true,
            elevation: 0,
            backgroundColor: controller.ifTransparent.value
                ? Colors.transparent
                : Colors.white,
            title: buildAppBar(),
          ),
          body: buildTabBarView(),
        ));
  }

  var tabTitle = [
    "热门",
    "推荐",
    "更新",
  ];
  Widget buildTabBar() {
    return Row(
      children: [
        SizedBox(
          child: TabBar(
            tabs: tabTitle.map((e) => Tab(text: e)).toList(),
            isScrollable: true,
            indicatorColor: Colors.transparent,
          ),
        )
      ],
    );
  }

  Widget buildTabBarView() {
    return TabBarView(
        children: tabTitle.map((e) {
      if (e == '热门') {
        return const PopularPage();
      } else if (e == '推荐') {
        return Recommend();
      } else if (e == '更新') {
        return Renew();
      } else {
        return Container();
      }
    }).toList());
  }

  Widget buildAppBar() {
    return Column(children: [
      controller.ifShowSearch.value ? searchBar() : SizedBox.shrink(),
      buildTabBar()
    ]);
  }

  Widget searchBar() {
    return GestureDetector(
        onTap: () {
          //logger.i("--搜索-");
        },
        child: Opacity(
          opacity: 0.9, //设置透明度
          child: Container(
              //作用于内部组件
              //margin: EdgeInsets.only(right: 15.w, left: 56.w),
              height: 25,
              //圆角颜色，背景
              decoration: const BoxDecoration(
                  borderRadius: BorderRadius.all(Radius.circular(20)),
                  color: Color.fromARGB(135, 180, 174, 174)),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text(
                    style: TextStyle(fontSize: 14, color: Color(0xD6000000)),
                    "搜索",
                    softWrap: false,
                    overflow: TextOverflow.ellipsis,
                  ),
                  IconButton(
                    padding: const EdgeInsets.all(0),
                    icon: const Icon(Icons.search),
                    onPressed: () => {},
                  ),
                ],
              )),
        ));
  }
}
